package hiatao.puzzle.adapter;

import haitao.puzzle.R;
import hiatao.puzzle.config.MyApplication;
import hiatao.puzzle.entity.User;
import hiatao.puzzle.utils.ImageUtils;
import hiatao.puzzle.utils.Tools;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListFollowAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<User> userList =new ArrayList<User>();
	private Context context;
	
	public ListFollowAdapter(Context context,  List<User> newsList) {
		if(newsList!=null){
			this.userList = newsList;
		}
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		
	}

	@Override
	public int getCount() {
		return userList==null ? 0:userList.size() ;
	}

	@Override
	public Object getItem(int position) {
		return userList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void appendToList(List<User> addList) {

		if (addList == null) {
			return;
		}
		if (userList == null) {
			userList = new ArrayList<User>();
		}
		userList.addAll(addList);
		notifyDataSetChanged();
	}
	
	public void clearAll() {
	    userList.clear();
	    notifyDataSetChanged();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PicModelHodler holder ;
		final User user = userList.get(position);
		if(convertView == null){
			holder = new PicModelHodler();
			convertView = mInflater.inflate(R.layout.list_follow, parent,false);
			holder.image = (ImageView) convertView.findViewById(R.id.head_image);
			holder.rank_img = (ImageView) convertView.findViewById(R.id.rank_img);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.zuan_tv = (TextView) convertView.findViewById(R.id.zuan_tv);
			holder.follow_tv = (TextView) convertView.findViewById(R.id.follow_tv);
			holder.intro_tv = (TextView) convertView.findViewById(R.id.intro_tv);
			convertView.setTag(holder);
		}
		else{
			holder = (PicModelHodler) convertView.getTag();
		}
		ImageUtils.loadHeadImg(user.getHeadUrl(), holder.image);
		holder.name_tv.setText(user.getName());
		holder.intro_tv.setText(user.getIntro());
		holder.follow_tv.setText("NO"+(position+1));
		holder.zuan_tv.setText(""+(Tools.getRandom(1, 800)));
		
		if(position==0){
			holder.rank_img.setImageResource(R.drawable.ic_zuan_yellow);
		}else if(position==1) {
			holder.rank_img.setImageResource(R.drawable.ic_zuan_green);
		}else{
			holder.rank_img.setImageResource(R.drawable.ic_zuan_red);
		}
		if(MyApplication.getInstance().user==null){
			holder.follow_tv.setBackgroundResource(R.drawable.btn_follow_nor);
			holder.follow_tv.setTextColor(context.getResources().getColor(R.color.head_color));
			holder.follow_tv.setText("+关注");
			
			holder.follow_tv.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Tools.showToast("请先登陆!");
				}
				
			});
		}
		else{
			if(!user.isFocus()){
				holder.follow_tv.setBackgroundResource(R.drawable.btn_follow_nor);
				holder.follow_tv.setTextColor(context.getResources().getColor(R.color.head_color));
				holder.follow_tv.setText("+关注");
				final TextView playTv =holder.follow_tv;
				holder.follow_tv.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						playTv.setText("关注中...");
						playTv.setClickable(false);
						new Handler().postDelayed(new Runnable() {
							
							@Override
							public void run() {
								user.setFocus(true);
								playTv.setText("已关注");
								playTv.setBackgroundResource(R.drawable.btn_follow_press);
								playTv.setTextColor(context.getResources().getColor(R.color.white));
							}
						}, 500);
					}
				});
			}else{
				holder.follow_tv.setBackgroundResource(R.drawable.btn_follow_press);
				holder.follow_tv.setTextColor(context.getResources().getColor(R.color.white));
				holder.follow_tv.setText("已关注");
			}
			
		}

		return convertView;
	}

	private final class PicModelHodler {
		public ImageView image;
		public ImageView rank_img;
		public TextView name_tv;
		public TextView zuan_tv;
		public TextView follow_tv;
		public TextView intro_tv;
	}
}
